package com.ds.lens.data.service.dao;

import com.ds.lens.data.service.dao.entity.LensRole;
import com.ds.lens.data.service.dao.sql.provider.LensRoleSqlProvider;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface LensRoleDao {
    @Delete({
            "delete from lens_role",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
            "insert into lens_role (id, name, ",
            "creator, created_time, ",
            "mender, modified_time)",
            "values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, ",
            "#{creator,jdbcType=VARCHAR}, #{createdTime,jdbcType=TIMESTAMP}, ",
            "#{mender,jdbcType=VARCHAR}, #{modifiedTime,jdbcType=TIMESTAMP})"
    })
    int insert(LensRole record);

    @InsertProvider(type = LensRoleSqlProvider.class, method = "insertSelective")
    int insertSelective(LensRole record);

    @Select({
            "select",
            "id, name, creator, created_time, mender, modified_time",
            "from lens_role",
            "where id = #{id,jdbcType=BIGINT}"
    })
    @Results(id = "roleMap",value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "creator", property = "creator", jdbcType = JdbcType.VARCHAR),
            @Result(column = "created_time", property = "createdTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "mender", property = "mender", jdbcType = JdbcType.VARCHAR),
            @Result(column = "modified_time", property = "modifiedTime", jdbcType = JdbcType.TIMESTAMP)
    })
    LensRole selectByPrimaryKey(Long id);

    @UpdateProvider(type = LensRoleSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(LensRole record);

    @Update({
            "update lens_role",
            "set name = #{name,jdbcType=VARCHAR},",
            "creator = #{creator,jdbcType=VARCHAR},",
            "created_time = #{createdTime,jdbcType=TIMESTAMP},",
            "mender = #{mender,jdbcType=VARCHAR},",
            "modified_time = #{modifiedTime,jdbcType=TIMESTAMP}",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(LensRole record);

    @Select({
            "select",
            "role.id, role.name, role.creator,role.created_time as createdTime,role.mender,role.modified_time as modifiedTime ",
            "from lens_role role left join lens_user_role_relation ur on role.id=ur.role_id ",
            "where ur.user_name = #{userName,jdbcType=VARCHAR}"
    })
    List<LensRole> queryRoleByUserName(String userName);

    @Select({
            "select",
            "count(1) > 0 ",
            "from lens_role role left join lens_user_role_relation ur on role.id=ur.role_id ",
            "where role.name = #{roleName,jdbcType=VARCHAR} and ur.user_name = #{userName,jdbcType=VARCHAR}"
    })
    boolean isSysAdmin(@Param("roleName") String roleName,
                       @Param("userName") String userName);

    @Select({
            "select",
            "id, name, creator, created_time, mender, modified_time",
            "from lens_role",
            "where name = #{roleName,jdbcType=VARCHAR}"
    })
    @ResultMap("roleMap")
    LensRole queryRoleByName(String roleName);
}
